package com.health.mapp.bean.rongcloud;

import java.awt.List;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;

import com.health.db.model.IMsgIMMessage;
import com.health.mapp.utils.Constant;
import com.health.mapp.utils.DateUtils;

public class RongCloudHelper {

 

	public static IMsgIMMessage parseObject(String req) {
		IMsgIMMessage message = new IMsgIMMessage();
		message.setCatalogid(50l);
		message.setSystemid(1);
		// 设置数据是否转换标识,0未处理，1处理中，2处理成功，3处理失败
		message.setIsDeleteFlag(Constant.CONSTANTS_RONGCLOUDCALLBACK_MSG_PARSE_STATE_0);
		message.setSourceid(1);

		int a = req.indexOf("&channelType");
		int b = req.indexOf("&fromUserId");
		int c = req.indexOf("&toUserId");
		int d = req.indexOf("&msgTimestamp");
		int e = req.indexOf("&timestamp");
		int f = req.indexOf("&objectName");
		int g = req.indexOf("&content");
		int h = req.indexOf("&msgUID");
		//System.out.println(a);
		//System.out.println(b);
		//System.out.println(c);
		//System.out.println(d);
		//System.out.println(e);
		//System.out.println(f);
		//System.out.println(g);
		//System.out.println(h);
		
		//the first node does not start with & ,so,the index is -1.
		if(a<0)
		{
			a=0;
		}
		if(b<0)
		{
			b=0;
		}
		if(c<0)
		{
			c=0;
		}
		if(d<0)
		{
			d=0;
		}
		if(e<0)
		{
			e=0;
		}
		if(f<0)
		{
			f=0;
		}
		if(g<0)
		{
			g=0;
		}
		if(h<0)
		{
			h=0;
		}
		 
		
		// sort index
		ArrayList<Integer> nums = new ArrayList<Integer>();
		nums.add(a);
		nums.add(b);
		nums.add(c);
		nums.add(d);
		nums.add(e);
		nums.add(f);
		nums.add(g);
		nums.add(h);
		//System.out.println(nums);
		Collections.sort(nums, new Comparator<Integer>() {

			public int compare(Integer left, Integer rigth) {

				// 降序
				if (left > rigth) {

					return -1;
				} else if (left < rigth) {

					return 1;
				} else {

					return 0;
				}
			}
		});
		 //System.out.println("after sort:"+nums);

		
		 
		for(int i=0;i<nums.size();i++ )
		{
		 
			int currIndex = nums.get(i);
			String currStr=req.substring(currIndex);
			//System.out.println("currStr:"+currStr);
			prepareMessage(message,currStr);
		
			req = getRemainStr(req, currIndex);
			//System.out.println("RemainStr:"+req);
		}
		
		
		//System.out.println(message);
		return message;

	}

	public static void prepareMessage(IMsgIMMessage message,String currReq)
	{
		try {
				// 对象映射器
				ObjectMapper objectMapper = new ObjectMapper();
				objectMapper.disable(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES);
				objectMapper.configure(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS,	false);

	 
			 
				String tempPara = currReq;
				
				
				
				if (tempPara.contains("fromUserId")) {

					String fromUserId= tempPara.substring(tempPara.indexOf("fromUserId")+11);
					
					message.setSenderid(Long.valueOf(fromUserId));

				}  
				if (tempPara.contains("toUserId")) {

					String toUserId= tempPara.substring(tempPara.indexOf("toUserId")+9);
					message.setGroupid(Long.valueOf(toUserId));

				} 
				if (tempPara.contains("objectName")) {

					String objectName =tempPara.substring(tempPara.indexOf("objectName")+11);
					message.setMediatype(objectName);

				} else if (tempPara.contains("content")) {

					String content = tempPara.substring(tempPara.indexOf("content")+8);
					 //System.out.println("content:"+content);
					
					//暴力去掉特殊的表情符
					 content = content.replaceAll("[\\x{00}-\\x{FF}]", "");
					 // 要映射为的对象
					Content jsonBean = null;
					Class<?> reqClass = Class.forName("com.health.mapp.bean.rongcloud.Content");
					
						jsonBean = (Content) objectMapper.readValue(content,reqClass);
				
                     // System.out.println("jsonBean:"+jsonBean.getContent());
					if (null != jsonBean) {
						if (null != jsonBean.getUser()) {
							message.setReceiverid(Long.valueOf(jsonBean
									.getUser().getId()));
							message.setSenderName(jsonBean.getUser().getName());
						}
						message.setMessageText(jsonBean.getContent());

						message.setMediaUrl(jsonBean.getImageUri());
					}

				} else if (tempPara.contains("timestamp")) {

					String  timestamp  = tempPara.substring(tempPara.indexOf("timestamp")+10);
					message.setItemData(timestamp );

				} else if (tempPara.contains("channelType")) {

					String channelType  = tempPara.substring(tempPara.indexOf("channelType")+12);
					message.setModetypeid((short) (channelType
							.equals("GROUP") == true ? 1 : 2));

				} else if (tempPara.contains("msgTimestamp")) {

					String msgTimestamp  =tempPara.substring(tempPara.indexOf("msgTimestamp")+13);
					message.setMessageTime(DateUtils.now(Long.valueOf(msgTimestamp)));

				} else if (tempPara.contains("msgUID")) {

					String  msgUID  =tempPara.substring(tempPara.indexOf("msgUID")+7);
					message.setSourcevoucherid(msgUID);

				}

		} catch (JsonParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonMappingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		 

	}
	public static String getRemainStr(String req, int endInedx) {

		String remainStr = req.substring(0, endInedx);
		return remainStr;

	}

	public static void main(String agrs[]) {

		String req1 = "channelType=GROUP&fromUserId=69&toUserId=28326&msgTimestamp=1494407751008&timestamp=1494407751008&objectName=RC:TxtMsg&content={\"content\":\"12121212&1212121212\",\"user\":{\"name\":\"12122&1212\",\"icon\":\"http://shouer.oss-cn-beijing.aliyuncs.com/docImage/kefu.png\",\"id\":\"69\"}}&msgUID=5DV3-955C-0C1D-LKDH";
		String req2="channelType=GROUP&fromUserId=69&toUserId=28326&msgTimestamp=1494409210899&timestamp=1494409210899&objectName=RC:VcMsg&content={\"content\":\"IyFBTVIKLNEafAAeef/hgmeAH8AD/+ggggAALMWpzAAf+f/hgmYAH8AD/+ggggAALIqkvCI/sXVjsGZCH4BG3+ggh4AALHUNMw9xBFtwYL+zl8Gx/sXRVl5mLE9/KYcgCkpkR/Yh3lRm4mpSAdpMLFKLKYZwWFYFx+2t+5eQGgY4yeGILBI3dw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6Fu2x+YeGSsC04UWpCxFoCeSLBuV2h/nh+Hs+UaTpHm/WkBY551oLC6prD3+J+YYEMU8zkNVO1o8bLNWLCyoRw7yx+YGWakvOkp1iahqYinULC2SvJYyx+y12QNF5R5e2JGB0TZ8LFBy54Zv79NuybRF5MPXG4MfCR7YLGGTGeAb0qsoWjIysRFl2M0IUTYKLESVE2gFmBNI+LG2cL8BdndW5St8LFFyoB/rWB4bqBFC7GtSehS+rNKkLFKLHj3OOGzyb//6HeNTMGB5j9S2\",\"duration\":1,\"user\":{\"name\":\"???????\",\"icon\":\"http://shouer.oss-cn-beijing.aliyuncs.com/docImage/kefu.png\",\"id\":\"69\"}}&msgUID=5DV3-ENC2-CC1D-LKDH";
		String req3="channelType=GROUP&fromUserId=69&toUserId=28326&msgTimestamp=1494409193985&timestamp=1494409193985&objectName=RC:ImgMsg&content={\"content\":\"/9j/4AAQSkZJRgABAQAASABIAAD/4QBYRXhpZgAATU0AKgAAAAgAAgESAAMAAAABAAEAAIdpAAQAAAABAAAAJgAAAAAAA6ABAAMAAAABAAEAAKACAAQAAAABAAAAtKADAAQAAAABAAAA8AAAAAD/7QA4UGhvdG9zaG9wIDMuMAA4QklNBAQAAAAAAAA4QklNBCUAAAAAABDUHYzZjwCyBOmACZjs+EJ+/8AAEQgA8AC0AwEiAAIRAQMRAf/EAB8AAAEFAQEBAQEBAAAAAAAAAAABAgMEBQYHCAkKC//EALUQAAIBAwMCBAMFBQQEAAABfQECAwAEEQUSITFBBhNRYQcicRQygZGhCCNCscEVUtHwJDNicoIJChYXGBkaJSYnKCkqNDU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6g4SFhoeIiYqSk5SVlpeYmZqio6Slpqeoqaqys7S1tre4ubrCw8TFxsfIycrS09TV1tfY2drh4uPk5ebn6Onq8fLz9PX29/j5+v/EAB8BAAMBAQEBAQEBAQEAAAAAAAABAgMEBQYHCAkKC//EALURAAIBAgQEAwQHBQQEAAECdwABAg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sSrKpO08N6VJuqowDjDDNNDFMIxznoTRcC7kUmar7jSb6LgWcijNVt9LvouBPmm1Dvo30XAkNNNN3Um6gANJSZpM0gHUlJRQA6jNNpaAHUoNMpc0ASA04Go80uaAJd1G6os0ZoA//Z\",\"user\":{\"name\":\"首儿健康\",\"icon\":\"http://shouer.oss-cn-beijing.aliyuncs.com/docImage/kefu.png\",\"id\":\"69\"},\"imageUri\":\"https://rongcloud-image.cn.ronghub.com/image_jpeg__RC-2017-05-10_5536_1494409192982?e=2147483647&token=livk5rb3__JZjCtEiMxXpQ8QscLxbNLehwhHySnX:PFSeW1pZf71oSBlKA8H1PmAKjKU=\"}&msgUID=5DV3-ELA0-4C1D-LKDH";
		parseObject(req3);
		 

	}

}
